<template>
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
          v-model="userAccount"
          name="userAccount"
          label="账号"
          :placeholder="`${user?.userAccount}`"
          :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
          v-model="oldPassword"
          type="password"
          name="oldPassword"
          label="旧密码"
          placeholder="请输入旧密码"
          :rules="[{ required: true, message: '请填写旧密码' }]"
      />
      <van-field
          v-model="userPassword"
          type="password"
          name="userPassword"
          label="新密码"
          placeholder="请输入新密码"
          :rules="[{ required: true, message: '请填写新密码' }]"
      />
      <van-field
          v-model="checkPassword"
          type="password"
          name="checkPassword"
          label="确认密码"
          placeholder="请确认密码"
          :rules="[{ required: true, message: '请填写确认密码' }]"
      />
    </van-cell-group>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>

  </van-form>
</template>

<script setup lang="ts">
import {onMounted, ref} from "vue";
import {getCurrentUser} from "../services/user";
import myAxios from "../plugins/myAxios";
import {Toast} from "vant";
import {useRoute} from "vue-router";

const user=ref();
const route = useRoute();

onMounted(async () => {
  user.value = await getCurrentUser();
})
const userAccount = ref('');
const oldPassword=ref('');
const userPassword = ref('');
const checkPassword = ref('');
const onSubmit = async () => {
  const res = await myAxios.post('/user/changePassword', {
    userAccount: userAccount.value,
    oldPassword: oldPassword.value,
    userPassword: userPassword.value,
    checkPassword: checkPassword.value
  })
  console.log(res, '用户修改密码');
  if (res.code === 0 && res.data) {
    Toast.success('修改密码成功');
    // 跳转到登录页面重新登录
    const redirectUrl = route.query?.redirect as string ?? '/user/login';
    window.location.href = redirectUrl;
  } else {
    Toast.fail('修改密码失败');
  }
};
</script>



<style scoped>

</style>